home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / imageviewer / ImageViewer.jar / ImageViewer.class (.txt) < prev   
Encoding:
Java Class File  |  2002-05-23  |  7.9 KB  |  212 lines

  1. import java.io.DataInputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Vector;
  5. import javax.microedition.io.Connector;
  6. import javax.microedition.io.HttpConnection;
  7. import javax.microedition.io.StreamConnection;
  8. import javax.microedition.lcdui.Alert;
  9. import javax.microedition.lcdui.AlertType;
  10. import javax.microedition.lcdui.Command;
  11. import javax.microedition.lcdui.CommandListener;
  12. import javax.microedition.lcdui.Display;
  13. import javax.microedition.lcdui.Displayable;
  14. import javax.microedition.lcdui.Form;
  15. import javax.microedition.lcdui.Image;
  16. import javax.microedition.lcdui.List;
  17. import javax.microedition.lcdui.TextField;
  18. import javax.microedition.midlet.MIDlet;
  19.  
  20. public class ImageViewer extends MIDlet implements CommandListener {
  21.    final String LABEL_COMMAND_EXIT = "Exit";
  22.    final String LABEL_COMMAND_MENU = "Menu";
  23.    final String LABEL_COMMAND_NEW = "New URL";
  24.    final String LABEL_COMMAND_EDIT = "Edit URL";
  25.    final String LABEL_COMMAND_DELETE = "Delete URL";
  26.    final String LABEL_COMMAND_OK = "Ok";
  27.    final String LABEL_COMMAND_CANCEL = "Cancel";
  28.    final String LABEL_NEWURL = "file:// New URL";
  29.    protected Display display;
  30.    protected List urlList;
  31.    protected Form editURLForm;
  32.    protected ImageCanvas canvas;
  33.    protected TextField labelTextField = new TextField("Label:", (String)null, 32, 0);
  34.    protected TextField urlTextField = new TextField("URL:", (String)null, 250, 4);
  35.    protected int editURLIndex;
  36.    protected Database urlDB;
  37.    protected Vector urls = new Vector();
  38.  
  39.    protected void showURL(String var1) {
  40.       byte[] var2 = null;
  41.       if (var1.startsWith("http:")) {
  42.          HttpConnection var3 = null;
  43.          DataInputStream var4 = null;
  44.  
  45.          try {
  46.             var3 = (HttpConnection)Connector.open(var1, 1);
  47.             int var5 = var3.getResponseCode();
  48.             if (var5 != 200) {
  49.                throw new IOException("HTTP Response Code = " + var5);
  50.             }
  51.  
  52.             int var6 = (int)var3.getLength();
  53.             if (var6 <= 0) {
  54.                throw new IOException("Content length is missing");
  55.             }
  56.  
  57.             var4 = var3.openDataInputStream();
  58.             var2 = new byte[var6];
  59.             var4.readFully(var2);
  60.          } catch (IOException var38) {
  61.             this.display.setCurrent(new Alert("Error", "Unable to read from URL!", (Image)null, AlertType.ERROR));
  62.          } finally {
  63.             try {
  64.                if (var4 != null) {
  65.                   var4.close();
  66.                }
  67.  
  68.                if (var3 != null) {
  69.                   var3.close();
  70.                }
  71.             } catch (IOException var34) {
  72.             }
  73.  
  74.          }
  75.       } else {
  76.          StreamConnection var40 = null;
  77.          InputStream var42 = null;
  78.  
  79.          try {
  80.             var40 = (StreamConnection)Connector.open(var1, 1);
  81.             var42 = var40.openInputStream();
  82.             if (var42.available() > 0) {
  83.                var2 = new byte[var42.available()];
  84.                var42.read(var2);
  85.             }
  86.          } catch (IOException var36) {
  87.             this.display.setCurrent(new Alert("Error", "Unable to read from URL!", (Image)null, AlertType.ERROR));
  88.          } finally {
  89.             try {
  90.                if (var42 != null) {
  91.                   var42.close();
  92.                }
  93.  
  94.                if (var40 != null) {
  95.                   var40.close();
  96.                }
  97.             } catch (IOException var33) {
  98.             }
  99.  
  100.          }
  101.       }
  102.  
  103.       if (var2 != null) {
  104.          try {
  105.             Image var41 = Image.createImage(var2, 0, var2.length);
  106.             if (this.canvas == null) {
  107.                this.canvas = new ImageCanvas();
  108.                this.canvas.addCommand(new Command("Exit", 7, 1));
  109.                this.canvas.addCommand(new Command("Menu", 2, 2));
  110.                this.canvas.setCommandListener(this);
  111.             }
  112.  
  113.             this.canvas.setImage(var41);
  114.             this.display.setCurrent(this.canvas);
  115.          } catch (Exception var35) {
  116.             this.display.setCurrent(new Alert("Error", "Unable to decode image!", (Image)null, AlertType.ERROR));
  117.          }
  118.       }
  119.  
  120.    }
  121.  
  122.    public void commandAction(Command var1, Displayable var2) {
  123.       String var3 = var1.getLabel();
  124.       int var4 = var1.getCommandType();
  125.       if (var3 == "Exit") {
  126.          this.destroyApp(false);
  127.       } else if (var1 == List.SELECT_COMMAND) {
  128.          this.showURL(this.getURL(this.urlList.getSelectedIndex())[1]);
  129.       } else if (var3 == "Menu") {
  130.          this.showURLList();
  131.       } else if (var3 == "New URL") {
  132.          this.urlDB.addRecord("file:// New URL".getBytes());
  133.          this.urls.addElement("file:// New URL");
  134.          this.editURLIndex = this.urls.size() - 1;
  135.          this.showEditURLForm();
  136.       } else if (var3 == "Edit URL") {
  137.          this.editURLIndex = this.urlList.getSelectedIndex();
  138.          this.showEditURLForm();
  139.       } else if (var3 == "Ok") {
  140.          String var5 = this.urlTextField.getString().trim() + ' ' + this.labelTextField.getString();
  141.          this.urlDB.setRecord(this.urlDB.getId(this.editURLIndex), var5.getBytes());
  142.          this.urls.setElementAt(var5, this.editURLIndex);
  143.          this.showURLList();
  144.       } else if (var3 == "Cancel") {
  145.          this.showURLList();
  146.       } else if (var3 == "Delete URL") {
  147.          this.urlDB.deleteRecord(this.urlDB.getId(this.urlList.getSelectedIndex()));
  148.          this.urls.removeElementAt(this.urlList.getSelectedIndex());
  149.          this.showURLList();
  150.       }
  151.  
  152.    }
  153.  
  154.    String[] getURL(int var1) {
  155.       String var2 = (String)this.urls.elementAt(var1);
  156.       int var3 = var2.indexOf(32);
  157.       String[] var4 = new String[]{var2.substring(var3 + 1), var2.substring(0, var3)};
  158.       return var4;
  159.    }
  160.  
  161.    void showEditURLForm() {
  162.       if (this.editURLForm == null) {
  163.          this.editURLForm = new Form("Edit URL");
  164.          this.editURLForm.append(this.labelTextField);
  165.          this.editURLForm.append(this.urlTextField);
  166.          this.editURLForm.addCommand(new Command("Ok", 4, 0));
  167.          this.editURLForm.addCommand(new Command("Cancel", 2, 1));
  168.          this.editURLForm.setCommandListener(this);
  169.       }
  170.  
  171.       String[] var1 = this.getURL(this.editURLIndex);
  172.       this.labelTextField.setString(var1[0]);
  173.       this.urlTextField.setString(var1[1]);
  174.       this.display.setCurrent(this.editURLForm);
  175.    }
  176.  
  177.    void showURLList() {
  178.       this.urlList = new List("Image Viewer", 3);
  179.  
  180.       for(int var1 = 0; var1 < this.urls.size(); ++var1) {
  181.          this.urlList.append(this.getURL(var1)[0], (Image)null);
  182.       }
  183.  
  184.       this.urlList.addCommand(new Command("Exit", 2, 0));
  185.       this.urlList.addCommand(new Command("New URL", 1, 1));
  186.       this.urlList.addCommand(new Command("Edit URL", 8, 2));
  187.       this.urlList.addCommand(new Command("Delete URL", 8, 3));
  188.       this.urlList.setCommandListener(this);
  189.       this.display.setCurrent(this.urlList);
  190.    }
  191.  
  192.    protected void startApp() {
  193.       this.display = Display.getDisplay(this);
  194.       this.urlDB = new Database("URLs");
  195.  
  196.       for(int var1 = 0; var1 < this.urlDB.numRecords(); ++var1) {
  197.          this.urls.addElement(new String(this.urlDB.getRecord(this.urlDB.getId(var1))));
  198.       }
  199.  
  200.       this.showURLList();
  201.    }
  202.  
  203.    protected void pauseApp() {
  204.    }
  205.  
  206.    protected void destroyApp(boolean var1) {
  207.       this.urlDB.close();
  208.       this.display.setCurrent((Displayable)null);
  209.       ((MIDlet)this).notifyDestroyed();
  210.    }
  211. }
  212.